package com.lh.arithmetic.sortarithmetic;

import com.lh.array.HighArray;

/**
 ************************************************
 ** Module : 二分法查找
 ** Date: 2012-8-20  Time: 下午05:24:29 
 ** Author lh 
 ************************************************ 
 */
public class DichotomyFind  extends HighArray{

	public DichotomyFind(int pacity) {
		super(pacity);
	}	
	
	public int findByDichotomy(long searchKey){	
		int lowerBound = 0;
		int upperBound = nElems-1;
		int curIn;
		while (true) {
			curIn  = (lowerBound + upperBound)/2;
			if(searchKey == arry[curIn]){
			    return curIn;	
			}else if(lowerBound>upperBound){// not find 
				return nElems;
			}else {
				if(arry[curIn]<searchKey){
					lowerBound = curIn+1;
				}else {
					upperBound = curIn - 1;
				}
			}
			
		}
	}

}
